/**  
* @Title: ShopPrintController.java
* @Package com.lemon.controller.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年11月14日
* @version V1.0  
*/
package com.lemon.controller.cityRun;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.lemon.conf.MyException;
import com.lemon.conf.MyInterceptor.LoginRequired;
import com.lemon.controller.BaseController;
import com.lemon.model.cityRun.CrShopPrinter;
import com.lemon.service.cityRun.ShopPrintService;
import com.lemon.utils.ReqResult;
import com.lemon.utils.XMLUtils;
import com.lemon.utils.printer.ElinkPrinter;

/**
 * <p>Title: ShopPrintController</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年11月14日
 * @版本 V 1.0 
 */
@RestController
@RequestMapping("/print")
public class ShopPrintController extends BaseController {

	@Resource
	private ShopPrintService printService;

	/**
	 * <b>Description:<blockquote> 获取打印机列表
	 * </blockquote></b>
	 * 
	 * @Title: getShopPrintList
	 * @param printer
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月15日
	 */
	@RequestMapping("/getShopPrintList")
	public ReqResult getShopPrintList(@ModelAttribute CrShopPrinter printer , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			//			CrShopUser u = (CrShopUser) request.getSession().getAttribute("user");
			//			long ri = IdTypeHandler.decode(u.getRuleId());
			/*
			 * if(ri == 0 || ri == 2) {
			 * reqResult.setReturnResult("权限不足",ReqResult.code_no_permission);
			 * return reqResult;
			 * }
			 */
			StringBuffer buffer = new StringBuffer();
			buffer.append("  and a.status != 0  ");
			printer.setWhere(buffer.toString());
			printer.setOrderBy(" order by a.create_time desc ");
			List<CrShopPrinter> list = printService.selectPrintListBySelective(printer);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 获取打印机列表
	 * </blockquote></b>
	 * 
	 * @Title: getShopPrintList
	 * @param printer
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月15日
	 */
	@RequestMapping("/getShopPrintListByOn")
	public ReqResult getShopPrintListByOn(@ModelAttribute CrShopPrinter printer , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			//			CrShopUser u = (CrShopUser) request.getSession().getAttribute("user");
			//			long ri = IdTypeHandler.decode(u.getRuleId());
			/*
			 * if(ri == 0 || ri == 2) {
			 * reqResult.setReturnResult("权限不足",ReqResult.code_no_permission);
			 * return reqResult;
			 * }
			 */
			StringBuffer buffer = new StringBuffer();
			buffer.append("  and a.status = 1  ");
			printer.setWhere(buffer.toString());
			printer.setOrderBy(" order by a.create_time desc ");
			List<CrShopPrinter> list = printService.selectPrintListBySelective(printer);
			for (int i = 0; i < list.size(); i++) {
				CrShopPrinter printer2 = list.get(i);
				if (printer2.getType() == 3) {
					Cookie cookie = new Cookie("pr_type", "3");
					cookie.setPath("/");
					response.addCookie(cookie);
				}
			}
			String jsonString = XMLUtils.getXmlToJSONString(XMLUtils.setUrl);
			JSONObject parse = (JSONObject) JSONObject.parse(jsonString);
			JSONObject js = parse.getJSONObject("sets").getJSONObject("sysSet");
			Cookie cookie = new Cookie("kf", js.getString("platPhone"));
			cookie.setPath("/");
			response.addCookie(cookie);
			reqResult.setReturnResult("");
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote>根据id 查询打印机信息
	 * </blockquote></b>
	 * 
	 * @Title: getShopPrintById
	 * @param printer
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月15日
	 */
	@RequestMapping(value = "/getShopPrintById" , method = RequestMethod.POST)
	public ReqResult getShopPrintById(@ModelAttribute CrShopPrinter printer , HttpServletRequest request ,
			HttpServletResponse response) {

		ReqResult reqResult = new ReqResult();
		try {
			//			CrShopUser u = (CrShopUser) request.getSession().getAttribute("user");
			//			long ri = IdTypeHandler.decode(u.getRuleId());
			/*
			 * if(ri == 0 || ri == 2) {
			 * reqResult.setReturnResult("权限不足",ReqResult.code_no_permission);
			 * return reqResult;
			 * }
			 */
			StringBuffer buffer = new StringBuffer();
			buffer.append("  and a.status != 0 ");
			printer.setWhere(buffer.toString());
			List<CrShopPrinter> list = printService.selectPrintListBySelective(printer);
			reqResult.setReturnResult(list.size() > 0 ? list.get(0) : null);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 添加打印机
	 * </blockquote></b>
	 * 
	 * @Title: addShopPrintInfo
	 * @param printer
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月15日
	 */
	@LoginRequired
	@RequestMapping(value = "/addShopPrintInfo.do" , method = RequestMethod.POST)
	public ReqResult addShopPrintInfo(@ModelAttribute CrShopPrinter printer , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			if (printer.getType() != 2) {
				printer.setBrand(null);
			} else {
				if (printer.getBrand() == 2) {
					//先将设备添加到易连云管理后台
					String code = ElinkPrinter.addClient(printer.getDevNo() , printer.getDevSecretKey() ,
							printer.getTitle());
					if (code != null && code.length() > 1) {
						JSONObject js = (JSONObject) JSONObject.parse(code);
						String cd = js.getString("errno");
						if ("-1.6".equals(cd)) {
							reqResult.setReturnResult("打印机编码或打印机秘钥填写错误" , ReqResult.code_error);
							return reqResult;
						}
					}
				}
			}
			int row = printService.insertPrintSelective(printer);
			reqResult.setReturnResult(row);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "添加打印机失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 修改打印机
	 * </blockquote></b>
	 * 
	 * @Title: updateShopPrintInfo
	 * @param printer
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月15日
	 */
	@LoginRequired
	@RequestMapping(value = "/updateShopPrintInfo.do" , method = RequestMethod.POST)
	public ReqResult updateShopPrintInfo(@ModelAttribute CrShopPrinter printer , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			if (printer.getBrand() != null && printer.getType() != 2 && printer.getBrand() == 2) {
				//先将设备添加到易连云管理后台
				String code = ElinkPrinter.addClient(printer.getDevNo() , printer.getDevSecretKey() ,
						printer.getTitle());
				if (code != null && code.length() > 1) {
					JSONObject js = (JSONObject) JSONObject.parse(code);
					String cd = js.getString("errno");
					if ("-1.6".equals(cd)) {
						reqResult.setReturnResult("打印机编码或打印机秘钥填写错误" , ReqResult.code_error);
						return reqResult;
					}
				}
			}
			int row = printService.updatePrintSelective(printer);
			reqResult.setReturnResult(row);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}
}
